package com.lc.YYGH.cmn.controller;

import com.lc.YYGH.cmn.service.DictService;
import com.lc.YYGH.common.result.Result;
import com.lc.YYGH.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/admin/cmn/dict")

@Api(tags="数据字典管理")
public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation(value="根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id)
    {
        List<Dict> list =dictService.findChildData(id);
        return Result.success(list);
    }

    @ApiOperation(value="导出")
    @GetMapping(value="/exportData")
    public void exportData(HttpServletResponse response)
    {
        dictService.exportData(response);
    }

    @ApiOperation(value="导入")
    @PostMapping("importData")
    public Result importData(MultipartFile file)
    {
        dictService.importDictData(file);
        return Result.success();
    }


    @ApiOperation(value="获取数据字典名称")
    @GetMapping(value="/getName/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name="parentDictCode",value="上级编码",required=true)
            @PathVariable("parentDictCode")String parentDictCode,

            @ApiParam(name="value",value="值",required = true)
            @PathVariable("value") String value)
    {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode,value);
    }

    @ApiOperation(value="获取数据字典名称")
    @ApiImplicitParam(name="value",value="值",required=true,dataType ="Long",paramType = "path")
    @GetMapping(value="/getName/{value}")
    public String getName(
            @ApiParam(name="value",value="值",required = true)
            @PathVariable("value") String value)
    {
        return dictService.getNameByParentDictCodeAndValue("",value);

    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value="/findByDictCode/{dictCode}")
    public Result findDictByDictCode(
            @ApiParam(name="dictCode",value="节点编码",required = true)
            @PathVariable String dictCode)
    {
        List<Dict> list=dictService.findByDictCode(dictCode);
        return Result.success(list);

    }








}
